package com.lnsf.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.lnsf.dao.CommentsDao;
import com.lnsf.entity.Comments;
import com.lnsf.entity.Goods;
import com.lnsf.entity.User;
import com.lnsf.service.ICommentService;
import com.lnsf.service.IGoodsService;
import com.lnsf.service.IUserService;

/*
 * UserController层
 * author:黄华炎
 * 2017-07-26
 * 
 * 
 */

@Controller
@RequestMapping("/comments")
public class CommentsController {

	@Autowired
	private  ICommentService iCommentService;
	
	@Autowired
	private  IGoodsService iGoodsService;
	
	@Autowired
	private  IUserService iUserService;
	
	
	
	//传一个上层的评论，如果该评论属于是第一层评论，则传一个空的
	@RequestMapping("/saveComment")
	@ResponseBody
	public String saveComment(HttpSession session ,@RequestParam("preCommId") Integer preCommId, Comments currentComments){
		List<Comments> commentsList = new ArrayList<Comments>();
		//User user = (User) session.getAttribute("currentUser");
		
		//上一层为空，即现在保存的评论是第一层
		//if(comments.getCommnetsId()==0){
			Comments preComments = iCommentService.findCommById(preCommId);
			
			
			commentsList.add(preComments);
			currentComments.setParent(commentsList);
			iCommentService.saveComm(currentComments);
			iCommentService.updateCom(preCommId, currentComments);
			
			
		//}
		//else{
			
		//}
		
		return null;
	}
	
	//获得该商品的全部信息
	@RequestMapping("/findComments")
	@ResponseBody
	public List<Comments> findComments(HttpSession session ,Comments comments){
		
		//User user = (User) session.getAttribute("currentUser");
		
		//下面是测试
		Goods goods2 = iGoodsService.findOne(comments.getGoods().getId());
		System.out.println(goods2);
		//User user2 = iUserService.getUserByPhone(comments.getUser().getPhone());
		
		comments.setGoods(goods2);
		//comments.setUser(user2);
		List<Comments> list = iCommentService.findCommByUserAndGood(comments);
		System.out.println("list:"+list);
		return list;
	}
	
	
	
	
}
